// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <turbo/container/flat_hash_map.h>
#include <cantor/proto/function.pb.h>
#include <cantor/function/function_caller.h>
#include <turbo/utility/status.h>

namespace cantor {
    struct FunctionSet {
        FunctionSet() = default;

        ~FunctionSet() = default;

        std::string func_name;
        std::vector<std::shared_ptr<cantor::pb::catalog::FunctionDefine> > functions;
    };

    class FunctionRegistry {
    public:
        FunctionRegistry() = default;

        ~FunctionRegistry() = default;

        turbo::Status register_function(const std::shared_ptr<cantor::pb::catalog::FunctionDefine> & meta,
                                        const std::shared_ptr<FunctionCaller> &caller);

        std::shared_ptr<FunctionSet> find_function_set(const std::string &func_name) const;


        std::shared_ptr<FunctionCaller> find_caller(uint64_t id) const;

        std::shared_ptr<cantor::pb::catalog::FunctionDefine> find_function_define(uint64_t id) const;


        const turbo::flat_hash_map<std::string, std::shared_ptr<FunctionSet> > &function_sets() const {
            return _function_sets;
        }

        const turbo::flat_hash_map<uint64_t, std::shared_ptr<FunctionCaller> > &function_callers() const {
            return _function_callers;
        }

        const turbo::flat_hash_map<uint64_t, std::shared_ptr<cantor::pb::catalog::FunctionDefine> > &function_defines() const {
            return _function_defines;
        }

    private:
        turbo::flat_hash_map<std::string, std::shared_ptr<FunctionSet> > _function_sets;
        turbo::flat_hash_map<uint64_t, std::shared_ptr<FunctionCaller> > _function_callers;
        turbo::flat_hash_map<uint64_t, std::shared_ptr<cantor::pb::catalog::FunctionDefine> > _function_defines;
    };
} // namespace cantor
